package com.zhangqi.zhxa.base.impl;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.zhangqi.zhxa.MainActivity;
import com.zhangqi.zhxa.R;
import com.zhangqi.zhxa.base.BaseFragment;
import com.zhangqi.zhxa.domain.NewsMenuData;

import java.util.ArrayList;

/**
 * 侧边栏的Fragment
 * @author Zhang Qi
 * @date 2016-4-28
 */
public class LeftMenuFragment extends BaseFragment {

    @ViewInject(R.id.lv_list)
    private ListView lvList;

    private ArrayList<NewsMenuData.NewsData> mMenuList;

    private MenuAdapter mAdapter;

    private int mCurrentPos;//当前被选中的菜单位置

    @Override
    public View initView() {
        View view = View.inflate(mActivity, R.layout.fragment_left_menu,null);
        ViewUtils.inject(this,view);

        return view;
    }




    /**
     * 设置网络数据，此方法由NewsCenterPager调用来传递数据
     * @param data
     */
    public void setData(ArrayList<NewsMenuData.NewsData> data){
        mMenuList = data;

        mAdapter = new MenuAdapter();
        lvList.setAdapter(mAdapter);

        //设置点击事件
        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                mCurrentPos = position;
                mAdapter.notifyDataSetChanged();//刷新

                //通知新闻中心，切换页面
                setCurrentMenuDetailPager(position);

                //隐藏侧边栏
                toggle();
            }
        });
        mCurrentPos =0;//重置当前页面位置
    }

    /**
     * 切换菜单详情页页面
     * @param position
     */
    protected void setCurrentMenuDetailPager(int position){
        //获取新闻中心对象NewsCenterPager
        //1.先获取MainActivity ,
        // 2. 通过MainActivity获取ContentFragment ,
        // 3.通过ContentFragment获取NewsCenterPager
        MainActivity mainUI = (MainActivity) mActivity;
        ContentFragment contentFragment = mainUI.getContentFragment();
        NewsCenterPager newsCenterPager = contentFragment.getNewsCenterPager();

        //给新闻中心页面的FrameLayout填充布局
        newsCenterPager.setCurrentMenuDetailPager(position);

    }

    /**
     * 侧边栏展开或者收起的方法
     */
    private void toggle(){
        MainActivity mainUI = (MainActivity) mActivity;
        SlidingMenu slidingMenu = mainUI.getSlidingMenu();
        slidingMenu.toggle();//开关（如果开了，它就显示是关，如果关，它就开）
    }


    class MenuAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return mMenuList.size();
        }

        @Override
        public Object getItem(int position) {
            return mMenuList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View view = View.inflate(mActivity, R.layout.list_item_left_menu, null);

            TextView tvMenu = (TextView) view.findViewById(R.id.tv_menu);

            NewsMenuData.NewsData data = (NewsMenuData.NewsData) getItem(position);
            tvMenu.setText(data.title);

            if(mCurrentPos==position){
                //如果当前要绘制的item刚好是被选中的，需要设置为红色
                tvMenu.setEnabled(true);
            }else{
                //其他item都是白色
                tvMenu.setEnabled(false);
            }

            return view;
        }
    }
}
